package assists
{
	public class SunTimeCommute
	{
		private var days_of_month_1:Array=[31,28,31,30,31,30,31,31,30,31,30,31]; 
		private var sunTime:Array=[31,29,31,30,31,30,31,31,30,31,30,31]; 
		private var  h:Number=-0.833; 
		private	var UTo:Number=180.0; 
		private	var year:int;
		private	var month:int;
		private	var date:int;
		private	var glat:Number;
		private	var glong:Number;
		private var c:Array = [];
		private var rise:Number;
		private var set:Number;
		public function SunTimeCommute(latitude:Number,longitude:Number)
		{
			glat = latitude;
			glong = longitude;
		}
		
		public function getSunTime(yearValue:int,monthValue:int,dateValue:int):Object
		{
			year = yearValue; 
			month = monthValue; 
			date = dateValue; 
			rise=result_rise(UT_rise(UTo,GHA(UTo,G_sun(t_century(days(year,month,date),UTo)),ecliptic_longitude(L_sun(t_century(days(year,month,date),UTo)),G_sun(t_century(days(year,month,date),UTo)))),glong,e(h,glat,sun_deviation(earth_tilt(t_century(days(year,month,date),UTo)),ecliptic_longitude(L_sun(t_century(days(year,month,date),UTo)),G_sun(t_century(days(year,month,date),UTo)))))),UTo,glong,glat,year,month,date); 
			set=result_set(UT_set(UTo,GHA(UTo,G_sun(t_century(days(year,month,date),UTo)),ecliptic_longitude(L_sun(t_century(days(year,month,date),UTo)),G_sun(t_century(days(year,month,date),UTo)))),glong,e(h,glat,sun_deviation(earth_tilt(t_century(days(year,month,date),UTo)),ecliptic_longitude(L_sun(t_century(days(year,month,date),UTo)),G_sun(t_century(days(year,month,date),UTo)))))),UTo,glong,glat,year,month,date); 
			return output(rise,set,glong); 
		}
		//定义全局变量 
		
		private function leap_year(year:int):int{ 
			if(((year%400==0) || (year%100!=0) && (year%4==0))) return 1; 
			else return 0; 
		} 
		//判断是否为闰年:若为闰年，返回1；若非闰年，返回0 
		
		private function days(year:int,month:int, date:int):int{ 
			var  i:int=0; 
			var a:int = 0;
			for(i=2000;i<year;i++){ 
				if(leap_year(i)) a=a+366; 
				else a=a+365; 
			} 
			if(leap_year(year)){ 
				for(i=0;i<month-1;i++){ 
					a= a + sunTime[i]; 
				} 
			} 
			else { 
				for(i=0;i<month-1;i++){ 
					a=a+days_of_month_1[i]; 
				} 
			} 
			a=a+date; 
			return a; 
		} 
		//求从格林威治时间公元2000年1月1日到计算日天数days 
		
		private function  t_century(days:int,UTo:Number):Number
		{ 
			return (days+UTo/360)/36525; 
		} 
		//求格林威治时间公元2000年1月1日到计算日的世纪数t 
		
		private function L_sun(t_century:Number):Number{ 
			return (280.460+36000.770*t_century); 
		} 
		//求太阳的平黄径 
		
		private function G_sun(t_century:Number):Number{ 
			return (357.528+35999.050*t_century); 
		} 
		//求太阳的平近点角 
		
		private function ecliptic_longitude(L_sun:Number,G_sun:Number):Number{ 
			return (L_sun+1.915*Math.sin(G_sun*Math.PI/180)+0.02*Math.sin(2*G_sun*Math.PI/180)); 
		} 
		//求黄道经度 
		
		private function earth_tilt(t_century:Number):Number{ 
			return (23.4393-0.0130*t_century); 
		} 
		//求地球倾角 
		
		private function sun_deviation(earth_tilt:Number,ecliptic_longitude:Number):Number{ 
			return (180/Math.PI*Math.asin(Math.sin(Math.PI/180*earth_tilt)*Math.sin(Math.PI/180*ecliptic_longitude))); 
		} 
		//求太阳偏差 
		
		private function GHA(UTo:Number,G_sun:Number,ecliptic_longitude:Number):Number{ 
			return (UTo-180-1.915*Math.sin(G_sun*Math.PI/180)-0.02*Math.sin(2*G_sun*Math.PI/180)+2.466*Math.sin(2*ecliptic_longitude*Math.PI/180)-0.053*Math.sin(4*ecliptic_longitude*Math.PI/180)); 
		} 
		//求格林威治时间的太阳时间角GHA 
		
		private function e(h:Number,glat:Number,sun_deviation:Number):Number{ 
			return 180/Math.PI*Math.acos((Math.sin(h*Math.PI/180)-Math.sin(glat*Math.PI/180)*Math.sin(sun_deviation*Math.PI/180))/(Math.cos(glat*Math.PI/180)*Math.cos(sun_deviation*Math.PI/180))); 
		} 
		//求修正值e 
		
		private function UT_rise(UTo:Number,GHA:Number,glong:Number,e:Number):Number{ 
			return (UTo-(GHA+glong+e)); 
		} 
		//求日出时间 
		
		private function UT_set(UTo:Number,GHA:Number,glong:Number,e:Number):Number{ 
			return (UTo-(GHA+glong-e)); 
		} 
		//求日落时间 
		
		private function result_rise(UT:Number,UTo:Number,glong:Number,glat:Number,year:int,month:int,date:int):Number{ 
			var d:Number; 
			if(UT>=UTo) d=UT-UTo; 
			else d=UTo-UT; 
			if(d>=0.1) { 
				UTo=UT; 
				UT=UT_rise(UTo,GHA(UTo,G_sun(t_century(days(year,month,date),UTo)),ecliptic_longitude(L_sun(t_century(days(year,month,date),UTo)),G_sun(t_century(days(year,month,date),UTo)))),glong,e(h,glat,sun_deviation(earth_tilt(t_century(days(year,month,date),UTo)),ecliptic_longitude(L_sun(t_century(days(year,month,date),UTo)),G_sun(t_century(days(year,month,date),UTo)))))); 
				result_rise(UT,UTo,glong,glat,year,month,date); 
			} 
			return UT; 
		} 
		//判断并返回结果（日出） 
		
		private function result_set(UT:Number,UTo:Number,glong:Number,glat:Number,year:int,month:int,date:int):Number{ 
			var d:Number; 
			if(UT>=UTo) d=UT-UTo; 
			else d=UTo-UT; 
			if(d>=0.1){ 
				UTo=UT; 
				UT=UT_set(UTo,GHA(UTo,G_sun(t_century(days(year,month,date),UTo)),ecliptic_longitude(L_sun(t_century(days(year,month,date),UTo)),G_sun(t_century(days(year,month,date),UTo)))),glong,e(h,glat,sun_deviation(earth_tilt(t_century(days(year,month,date),UTo)),ecliptic_longitude(L_sun(t_century(days(year,month,date),UTo)),G_sun(t_century(days(year,month,date),UTo)))))); 
				result_set(UT,UTo,glong,glat,year,month,date); 
			} 
			return UT; 
		} 
		//判断并返回结果（日落） 
		
		private function Zone(glong:Number):int{ 
			if(glong>=0) return (int(glong/15.0)); 
			else return (int(glong/15.0)-1); 
		} 
		//求时区 
		
		private function output(rise:Number,setValue:Number,glong:Number):Object{ 
			  var aa:Number = setValue / 15;
//			trace(   int(int(set) / 15+Zone(glong)) + ": " + int(60*(aa+Zone(glong)-int(aa+Zone(glong))  ))    );
			
			var info:Object = {};
			var _zone:int = Zone(glong);
					if(int(60*(rise/15+_zone-int(rise/15+_zone)))<10) 
						info.sunRises = (int(rise/15+_zone) + ":0" + int( 60*(rise/15+_zone - int(rise/15+_zone))) ); 
					else 	info.sunRises = (int(rise/15+_zone) + ":" + int(60*(rise/15+_zone-int(rise/15+_zone)))); 
					if((60*(aa+Zone(glong)-int(aa+Zone(glong))))<10) 
						info.sunSet = int(int(set) / 15+Zone(glong)) + ":0" + int(60*(aa+Zone(glong)-int(aa+Zone(glong))  ))
					else 	info.sunSet = int(int(set) / 15+Zone(glong)) + ": " + int(60*(aa+Zone(glong)-int(aa+Zone(glong))  ));
              return info;		
		} 
		//打印结果 
		
	}
	
}